angular.module('searchModule', ['ionic'])
	.controller('searchCtrl', function($scope, searchR) {
		searchR.getKeywords().then(function(res) {

			$scope.keygoods = res.data.data;
		})
		searchR.getSearch().then(function(res) {

			$scope.search = res.data.data;
		})
		$scope.isshowgoods = true;
		$scope.isshowOrder = false;
		$scope.searchbtn = function() {
			$scope.search = '';
			$scope.isshowgoods = false;
			$scope.searchtext = $('#searchipt').val();
			if($scope.searchtext) {
				$scope.isshowOrder = true;
				$('.search_goods').addClass('ptop100');
			} else {
				$scope.isshowOrder = false;
			}
			searchR.getSearch($scope.searchtext).then(function(res) {

				$scope.search = res.data.data;
			})
		}
		$('.order_warp li').each(function(ind) {
			$(this).click(function() {
			$scope.search = '';
			$(this).addClass('o_active').siblings().removeClass('o_active');
				searchR.getSearch($scope.searchtext,ind).then(function(res) {
					$scope.search = res.data.data;
				})
			})
		});
		$scope.licick=function(i){	
			$scope.search = '';
			$scope.isshowOrder = true;
			$scope.isshowgoods = false;
			console.log($scope.keygoods.goods[i]);
			searchR.getSearch($scope.keygoods.goods[i]).then(function(res) {
					$scope.search = res.data.data;
			})
		}
		console.log($scope.search);


	})
	.factory("searchR", function($http) {
		//	https://ichuanyi.com/index.php?method=search.getKeywords
		var getKeywords = function() {
			return $http.get("/icy/index.php?method=search.getKeywords")
				.then(function(res) {
					return res
				})
		};
		var getSearch = function(i, sort = 0) {

			return $http.get("/icy/index.php?fromPageId=0&pageSize=20&method=goods.search&page=1", {
					params: {
						'keywords[]': i,
						'sortType': sort,
					}
				})
				.then(function(res) {
					return res
				})
		};

		return {
			getKeywords,
			getSearch
		}
	})